package io.dshubs.archives.controller;


import io.dshubs.archives.dao.ArchiveWarehouseLocationMapper;
import io.dshubs.archives.dao.ArchiveWarehouseMapper;
import io.dshubs.archives.entity.ArchiveBaseStore;
import io.dshubs.archives.entity.ArchiveWarehouse;
import io.dshubs.archives.entity.ArchiveWarehouseLocation;
import io.dshubs.archives.entity.ArchiveWarehouseZone;
import io.dshubs.archives.entity.vo.ArchiveWarehouseVo;
import io.dshubs.archives.service.ArchiveBaseStoreService;
import io.dshubs.archives.service.ArchiveWarehouseLocationService;
import io.dshubs.archives.service.ArchiveWarehouseService;
import io.dshubs.archives.service.ArchiveWarehouseZoneService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库房表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-17
 */
@Api(tags = "库房管理")
@RestController
@RequestMapping("/warehouse")
public class ArchiveWarehouseController {
    @Autowired
    private ArchiveWarehouseService archiveWarehouseService;

    @Autowired
    private ArchiveBaseStoreService archiveBaseStoreService;

    @Autowired
    private ArchiveWarehouseLocationService archiveWarehouseLocationService;


    @GetMapping("/page")
    @ApiOperation("库房管理:page")
    @LogOperation("库房管理:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "code", value = "库房编码", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "name", value = "库房名称", paramType = "query", dataType="String"),
    })
    public Result<PageData<ArchiveWarehouse>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveWarehouse>>().ok(archiveWarehouseService.page(params));
    }


    @PostMapping
    @ApiOperation("保存")
    @LogOperation("库房管理:save")
    public Result save(@RequestBody ArchiveWarehouse archiveWarehouse){
        //效验数据
        ValidatorUtils.validateEntity(archiveWarehouse, AddGroup.class, DefaultGroup.class);

        archiveWarehouseService.save(archiveWarehouse);
        return new Result();
    }

    @GetMapping("/{id}")
    @ApiOperation("查看详情")
    @LogOperation("库房管理:get")
    public Result<ArchiveWarehouse> get(@PathVariable Long id){
        return new Result<ArchiveWarehouse>().ok(archiveWarehouseService.selectById(id));
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("库房管理:update")
    public Result update(@RequestBody ArchiveWarehouse archiveWarehouse){
        //效验数据
        ValidatorUtils.validateEntity(archiveWarehouse, UpdateGroup.class, DefaultGroup.class);
        archiveWarehouseService.updateById(archiveWarehouse);
        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("库房管理:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveWarehouseService.delete(ids);
        return new Result();
    }
    @GetMapping("/simulation/pull-down")
    @ApiOperation("模拟库房-库房名称下拉")
    @LogOperation("库房管理:pullDown")
    public Result<List<ArchiveWarehouse>> pullDown(){
        return new Result<List<ArchiveWarehouse>>().ok(archiveWarehouseService.selectList());
    }


    @GetMapping("/simulation-page")
    @ApiOperation("模拟库房")
    @LogOperation("模拟库房:/warehouse/imitate:/img/icon-模拟库房.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "库房编码", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "name", value = "库房名称", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "warehouseId", value = "库房id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "fileNumber", value = "档号", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "segmentNo", value = "密集架号", paramType = "query", dataType="String"),
    })
    public Result<List<ArchiveWarehouseVo>> simulation(@ApiIgnore @RequestParam Map<String, Object> params){

        return new Result<List<ArchiveWarehouseVo>>().ok( archiveWarehouseService.simulation(params));
    }

    @GetMapping("/simulation-group")
    @ApiOperation("模拟库房显示所有密集架")
    @LogOperation("模拟库房：group")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId", value = "库房id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "warehouseZoneId", value = "库区id", paramType = "query", dataType="String")}
    )
    public Result<List<Map<String, Object>>> group(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<List<Map<String, Object>>>().ok(archiveWarehouseLocationService.group(params));
    }

    @GetMapping("/simulation-segment")
    @ApiOperation("模拟库房根据密集架查询所有的货物")
    @LogOperation("模拟库房：getBySegmentNo")
    public Result<List<ArchiveWarehouseLocation>> getBySegmentNo(@ApiIgnore @RequestParam Map<String, Object> params){

        return new Result<List<ArchiveWarehouseLocation>>().
                ok(archiveWarehouseLocationService.getBySegmentNo(params));
    }

    @GetMapping("/simulation-file")
    @ApiOperation("根据密集架id找到所有的档案盒")
    @LogOperation("模拟库房：getBySegmentId")
    public Result<List<ArchiveBaseStore>> getBySegmentId(Long locationId){

        return new Result<List<ArchiveBaseStore>>().
                ok(archiveBaseStoreService.getBySegmentId(locationId));
    }
}
